package com.A21MobileSales;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.A21.Adapter.CustomerProductPriceAdapter;
import com.A21.Object.A21CustomerPrice;
import com.A21.Object.MSTProduct;
import com.A21MobileSales.DataBase.DatabaseHelper;

public class CustomerProduct extends Activity {

	public static final int PRODUCT_DETAILS_ACTIVITY = 1;
	DatabaseHelper ObjDBHelper;
	ArrayList<A21CustomerPrice> lstCustomerProducts;
	Button btnDelSearch;
	EditText edtSearchProduct;
	ListView lvProduct;
	CustomerProductPriceAdapter productListAdapter;
	private String Code, CName;
	TextView tv_CName;
	Button btn_Home;
	ImageButton btn_back;
	Common objC;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customer_product);
		getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		try {
			ObjDBHelper = new DatabaseHelper(this); 
			objC=new Common(this);
			lvProduct = (ListView) findViewById(R.id.OLS_lvOrderLineList);
			if (getIntent().hasExtra("Code")) {
				Code = getIntent().getStringExtra("Code");
			}
			if (getIntent().hasExtra("CName")) {
				CName = getIntent().getStringExtra("CName");
			}
			
			lvProduct.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapter, View v,
						int Pos, long Id) {
				}
			});

			tv_CName = (TextView) findViewById(R.id.tv_CName);
			btn_Home = (Button) findViewById(R.id.btn_home);
			btn_Home.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent _intent = new Intent();
					_intent.setClass(CustomerProduct.this, HomeScreen.class);
					startActivity(_intent);
				}
			});

			btn_back = (ImageButton) findViewById(R.id.btn_back);
			btn_back.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Bundle b = new Bundle();
					Intent _intent = new Intent();
					_intent.putExtras(b);
					_intent.setClass(CustomerProduct.this, CustomerMaster.class);
					startActivity(_intent);
				}
			});
			tv_CName.setText(CName);

			edtSearchProduct = (EditText) findViewById(R.id.edtSearchProduct);
			btnDelSearch = (Button) findViewById(R.id.btn_del_search);
			btnDelSearch.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edtSearchProduct.setText("");
					//productListAdapter = new CustomerProductPriceAdapter(
					//		CustomerProduct.this, lstCustomerProducts,Code);
					//productListAdapter.notifyDataSetChanged();
					//lvProduct.setAdapter(productListAdapter);
				}
			});

			edtSearchProduct.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					try {
						
						String text = s.toString().toLowerCase(Locale.getDefault());
						productListAdapter.filter(text); 
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//lvProduct.setAdapter(new CustomerProductPriceAdapter(CustomerProduct.this, lstCustomerProducts,Code));
				}
			});  
			
			new ProductTask().execute();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
	public void bindData() { 
		productListAdapter = new CustomerProductPriceAdapter(
				CustomerProduct.this, lstCustomerProducts,Code);
		productListAdapter.notifyDataSetChanged();
		lvProduct.setAdapter(productListAdapter);
	}

	private class ProductTask extends AsyncTask<String, Void, Void> {

		private ProgressDialog dialog = new ProgressDialog(CustomerProduct.this);
		String StrMsg = null;

		protected void onPreExecute() {
			this.dialog.setMessage("Loading...");
			this.dialog.setCanceledOnTouchOutside(false);
			this.dialog.show();

		}

		protected Void doInBackground(final String... Params) { 
			try {
				ArrayList<MSTProduct>  lstProducts = ObjDBHelper.GetAllProducts(true);
				lstCustomerProducts=new ArrayList<A21CustomerPrice>();
				for (MSTProduct item : lstProducts) {
					A21CustomerPrice row=objC.GetA21CustomerPrice(Code,item.Product); 
					row.Product=item.Product;
					row.PName=item.PName;
					lstCustomerProducts.add(row);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			return null;
		}

		protected void onPostExecute(final Void unused) {
			if (this.dialog.isShowing()) {
				this.dialog.dismiss();
				this.dialog = null;
			}
			try { 
					bindData(); 
			} catch (Exception ex) {

				ex.printStackTrace();

			}

		}

	}

}